package com.sharp.core.math;

import java.util.Formatter;

public class UnVec3 {
	protected float x;
	protected float y;
	protected float z;
		
	/**
	    Creates a new Vector3D at (0,0,0).
	*/
	public UnVec3() {
	    
	}
	
	public UnVec3(UnVec3 from, UnVec3 to) {
		this( to.x - from.x, to.y - from.y, to.z - from.z );
	}
	
	
	/**
	    Creates a new Vector3D with the same values as the
	    specified Vector3D.
	*/
	public UnVec3(UnVec3 v) {
	    this(v.x, v.y, v.z);
	}
	
	
	/**
	    Creates a new Vector3D with the specified (x, y, z) values.
	*/
	public UnVec3(float x, float y, float z) {
	    setTo(x, y, z);
	}
	
	/**
	    Checks if this Vector3D is equal to the specified Object.
	    They are equal only if the specified Object is a Vector3D
	    and the two Vector3D's x, y, and z coordinates are equal.
	*/
	@Override
	public boolean equals(Object obj) {
		UnVec3 v = (UnVec3)obj;
	    return (v.x == x && v.y == y && v.z == z);
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Float.floatToIntBits(x);
		result = prime * result + Float.floatToIntBits(y);
		result = prime * result + Float.floatToIntBits(z);
		return result;
	}

	/**
	    Checks if this Vector3D is equal to the specified
	    x, y, and z coordinates.
	*/
	public boolean equals(float x, float y, float z) {
	    return (this.x == x && this.y == y && this.z == z);
	}
	
	
	
	private final void setTo(UnVec3 from, UnVec3 to) {
		setTo( to.x - from.x, to.y - from.y, to.z - from.z );
	}

	/**
	    Sets the vector to the same values as the specified
	    Vector3D.
	*/
	private final void setTo(UnVec3 v) {
	    setTo(v.x, v.y, v.z);
	}
	
	
	/**
	    Sets this vector to the specified (x, y, z) values.
	*/
	private final void setTo(float x, float y, float z) {
	    this.x = x;
	    this.y = y;
	    this.z = z;
	}
	
	/**
	    Returns the length of this vector as a float.
	*/
	public final float length() {
	    return Trig.sqrt(x*x + y*y + z*z);
	}
	
	/**
	    Converts this Vector3D to a String representation.
	*/
	@Override
	public String toString() {
		return new Formatter().format("(%.2f, %.2f, %.2f)", x, y, z).toString();
	}

	public final float getX() {
		return x;
	}

	public final float getY() {
		return y;
	}

	public final float getZ() {
		return z;
	}
}
